package com.vshining.grame;

import org.hibernate.cfg.ImprovedNamingStrategy;
import org.hibernate.cfg.NamingStrategy;

/**
 * @author "Shining Wang<shiningwang@vshining.com>"
 */
public class GrameNamingStrategy implements NamingStrategy {

    /**
     * A convenient singleton instance
     */
    @SuppressWarnings("unused")
    public static final NamingStrategy INSTANCE = new GrameNamingStrategy();

    @Override
    public String classToTableName(String className) {
        return getClassToTablePrefix() + "_" + improved.classToTableName(className).toUpperCase();
    }

    @Override
    public String propertyToColumnName(String propertyName) {
        return improved.propertyToColumnName(propertyName).toUpperCase();
    }

    @Override
    public String tableName(String tableName) {
        return improved.tableName(tableName).toUpperCase();
    }

    @Override
    public String columnName(String columnName) {
        return improved.columnName(columnName).toUpperCase();
    }

    @Override
    public String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable, String propertyName) {
        return improved.collectionTableName(ownerEntity, ownerEntityTable, associatedEntity, associatedEntityTable, propertyName).toUpperCase();
    }

    @Override
    public String joinKeyColumnName(String joinedColumn, String joinedTable) {
        return improved.joinKeyColumnName(joinedColumn, joinedTable).toUpperCase();
    }

    @Override
    public String foreignKeyColumnName(String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName) {
        return improved.foreignKeyColumnName(propertyName, propertyEntityName, propertyTableName, referencedColumnName).toUpperCase();
    }

    @Override
    public String logicalColumnName(String columnName, String propertyName) {
        return improved.logicalColumnName(columnName, propertyName).toUpperCase();
    }

    @Override
    public String logicalCollectionTableName(String tableName, String ownerEntityTable, String associatedEntityTable, String propertyName) {
        return improved.logicalCollectionTableName(tableName, ownerEntityTable, associatedEntityTable, propertyName).toUpperCase();
    }

    @Override
    public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
        return improved.logicalCollectionColumnName(columnName, propertyName, referencedColumn).toUpperCase();
    }

    private static NamingStrategy improved = ImprovedNamingStrategy.INSTANCE;

    private static final String DEFAULT_CLASS_TO_TABLE_PREFIX = "GRAME";

    private static String getClassToTablePrefix() {
        return System.getProperty("CLASS_TO_TABLE_PREFIX", DEFAULT_CLASS_TO_TABLE_PREFIX);
    }
}
